<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <!--
          表格
          el-table: data 绑定数据
          el-table-column: prop指定列  label列标题
      -->
      <el-table :data="cateList" stripe border>
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column label="分类" prop='cate_name'></el-table-column>
          <el-table-column label="别名" prop="cate_alias"></el-table-column>
          <el-table-column label="操作">
            <template #default="{row}">
                <!-- 点击修改 =>  获取当前数据的id   =>   发送axios获取当前这条数据  =>   放置到弹出框中编辑-->
                <!-- 作用域插槽: 获取组件内部传过来的数据 -->
                <el-button type="warning" size="mini" @click="edit(row.id)">修改</el-button>
                <el-button type="danger" size="mini" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加文章分类的弹出框  close事件可以控制关闭'×' -->
    <el-dialog :visible='addVisible' title="添加分类" width="35%" @close='handleAddClose'>

      <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="handleAddClose">取消</el-button>
        <el-button type="primary" @click="addCate">确定</el-button>
      </template>
    </el-dialog>

    <!-- 编辑文章分类的弹出框 -->
    <el-dialog :visible='editVisible' title="编辑分类"  width="35%" @close='handleEditClose'>

      <el-form :model="editForm" :rules="addRules" ref="editFormRef" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="handleEditClose">取消</el-button>
        <el-button type="primary" @click="clickUpdate">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [],
      // 控制添加分类
      addVisible: false,
      // 添加表单的数据对象
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },

      // 控制编辑分类
      editVisible: false,
      // 编辑表单的数据对象
      editForm: {}
    }
  },
  created () {
    this.getArtCateList()
  },
  methods: {
    // 获取文章分类列表
    async getArtCateList () {
      const { data: res } = await this.$http.get('/my/cate/list')
      this.cateList = res.data
    },

    // 弹出框关闭
    handleAddClose () {
      this.addVisible = false
      this.$refs.addRef.resetFields()
    },

    // 添加文章分类
    addCate () {
      this.$refs.addRef.validate(async valid => {
        if (!valid) return

        // axios发送请求
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)

        // 1.关闭弹出框
        this.handleClose()
        // 2.再次获取文章分类列表
        this.getArtCateList()
      })
    },

    // 关闭编辑弹出框
    handleEditClose () {
      this.editVisible = false
      this.$refs.editFormRef.resetFields()
    },

    // 根据id获取数据
    async edit (id) {
      // 1.根据id获取数据
      const { data: res } = await this.$http.get('/my/cate/info', { params: { id } })
      if (res.code === 0) {
        // 1.1 把当前的分类数据赋值给表单数据
        this.editForm = res.data
        // 1.2 编辑的弹出框显示
        this.editVisible = true
      }
    },

    // 更新文章分类
    clickUpdate () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return

        // axios更新分类
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)

        // 1.关闭更新弹出框
        this.handleEditClose()

        // 2.重新渲染分类信息
        this.getArtCateList()
      })
    },

    // 根据id删除分类
    async del (id) {
      if (id === 1 || id === 2) return this.$message.warning('不能删除该文章分类')

      const result = await this.$confirm('此操作将永久删除该文章分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      // 点击了取消 中止执行
      if (result === 'cancel') return

      const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)

      // 重新渲染分类页面
      this.getArtCateList()
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
